package com.algrithom.linktable;

import com.common.model.ListNode;

/**
 * 141. 环形链表
 * 
 * @author think
 */
public class Solution3 {
    
    public static void main(String[] args){
        Solution3 solution3 = new Solution3();
        //        ListNode<Integer> root = CommonUtil.createListNode(new Integer[]{3,2,0,-4});
        ListNode<Integer> root = new ListNode<>(1);
        System.out.println(solution3.hasCycle(root));
    }
    
    public boolean hasCycle(ListNode<Integer> head){
        if (head == null || head.getNext() == null) {
            return false;
        }
        ListNode<Integer> slow = head;
        ListNode<Integer> fast = head.getNext();
        while (slow != fast) {
            if (fast == null || fast.getNext() == null) {
                return false;
            }
            slow = slow.getNext();
            fast = fast.getNext().getNext();
        }
        return true;
    }
}
